//% color="#8B4513" iconWidth=50 iconHeight=40
namespace recognition{

    //% block="初始化摄像头直到成功 编号[CAMNUM]" blockType="command"
    //% CAMNUM.shadow="number" CAMNUM.defl="0"
    export function readcap(parameter: any, block: any) {
        let num=parameter.CAMNUM.code;
 
        Generator.addImport(`import cv2\n`)
    
        Generator.addCode(`vd = cv2.VideoCapture(${num})`)
            
        Generator.addCode(`while not vd.isOpened():
    continue`)    
    }


    //% block="创建窗口[WIN]设置为全屏" blockType="command"
    //% WIN.shadow="string" WIN.defl="cvwindow"
    export function resizeWindow(parameter: any, block: any) {
        let win=parameter.WIN.code;

        Generator.addCode(`cv2.namedWindow(${win},cv2.WND_PROP_FULLSCREEN)`)
        
        Generator.addCode(`cv2.setWindowProperty(${win}, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)`)
  
    }

    // //% block="创建窗口名[WIN]设置宽[W]高[H]" blockType="command"
    // //% WIN.shadow="string" WIN.defl="cvwindow"
    // //% W.shadow="normal" W.defl="240"
    // //% H.shadow="normal" H.defl="150"
    // export function namedWindow(parameter: any, block: any) {
    //     let win=parameter.WIN.code;
    //     let w=parameter.W.code;
    //     let h=parameter.H.code;
    //     Generator.addCode(`cv2.namedWindow(${win},0)`)
    //     Generator.addCode(`cv2.resizeWindow(${win}, (${w}, ${h}))`)
        
    // }
    // //% block="窗口名[WIN]移动x到[X] Y到[Y]" blockType="command"
    // //% WIN.shadow="string" WIN.defl="cvwindow"
    // //% X.shadow="normal" X.defl="0"
    // //% Y.shadow="normal" Y.defl="150"
    // export function moveWindow(parameter: any, block: any) {
    //     let win=parameter.WIN.code;
    //     let x=parameter.X.code;
    //     let y=parameter.Y.code;

    //     Generator.addCode(`cv2.moveWindow(${win}, ${x}, ${y})`)
        
    // }
   //% block="---"
   export function noteSep() {

}
    //% block="从VideoCapture对象[VD]中抓取下一帧[IMG]以及状态[RET]" blockType="command"
    //% VD.shadow="normal" VD.defl="vd"
    //% IMG.shadow="normal" IMG.defl="img"
    //% RET.shadow="normal" RET.defl="ret"
    export function readimg(parameter: any, block: any) {
        let vd=parameter.VD.code;
        let img=parameter.IMG.code;
        let ret=parameter.RET.code;
        Generator.addCode(`${ret}, ${img} = ${vd}.read()`)
        
    }

    //% block="对象名[IMG]将图片[IMG_1]剪裁为宽[W]高[H]" blockType="command"
    //% IMG.shadow="normal" IMG.defl="img_src"
    //% IMG_1.shadow="normal" IMG_1.defl="img"
    //% W.shadow="normal" W.defl="240"
    //% H.shadow="normal" H.defl="320"

    export function resizeImage(parameter: any, block: any) {
        let img=parameter.IMG.code;
        let img_1=parameter.IMG_1.code;
        let w=parameter.W.code;
        let h=parameter.H.code;

 
//         Generator.addCode(`cvimg_h, cvimg_w, cvimg_c = ${img_1}.shape
// cvimg_w1 = cvimg_h*${w}//${h}
// cvimg_x1 = (cvimg_w-cvimg_w1)//2
// ${img} = ${img_1}[:, cvimg_x1:cvimg_x1+cvimg_w1]
// ${img} = cv2.resize( ${img}, (${w}, ${h}))
// `)
            Generator.addCode(`xmin, ymin, w, h = 250,100,${w},${h} 
${img} = ${img_1}[ymin:ymin+h, xmin:xmin+w]`)
    }
    //% block="对象名[IM]将图片[IMAGE]使用BGR和RGB之间进行转换" blockType="command"
    //% IM.shadow="normal" IM.defl="imgROI"
    //% IMAGE.shadow="normal" IMAGE.defl="img_src"
    export function cvWindow(parameter: any, block: any) {
        let im=parameter.IM.code;
        let image=parameter.IMAGE.code;
        Generator.addImport(`from PIL import Image`)
        
        Generator.addCode(`${im} = Image.fromarray(cv2.cvtColor(${image},cv2.COLOR_BGR2RGB))`)
  
    } 

    
    //% block="---"
    export function noteSep1() {

    }

    //% block="显示图片[IMG]设置名称[WN]" blockType="command"
    //% IMG.shadow="normal" IMG.defl="img_src"
    //% WN.shadow="string" WN.defl="cvwindow"
    export function showimg(parameter: any, block: any) {
        let img=parameter.IMG.code;
        let wm=parameter.WN.code;
        Generator.addCode(`cv2.imshow(${wm}, ${img})
cv2.waitKey(20)`) 
        
    }    

    //% block="保存图片[IMG]到[PATH]" blockType="command"
    //% IMG.shadow="normal" IMG.defl="img_src"
    //% PATH.shadow="string" PATH.defl="Mind+.png"
    export function saveimg(parameter: any, block: any) {
        let img=parameter.IMG.code;
        let path=parameter.PATH.code;


        Generator.addCode(`cv2.imwrite(${path}, ${img})`)
        
 
    } 
    //% block="---"
    export function noteSep2() {

    }
    //% block="等待按键[BUTTON]时间[TIME]毫秒" blockType="boolean"
    //% BUTTON.shadow="normal" BUTTON.defl="a"
    //% TIME.shadow="normal" TIME.defl="20"
    export function button(parameter: any, block: any) {
        let button=parameter.BUTTON.code;
        let time=parameter.TIME.code;
        Generator.addCode(`cv2.waitKey(${time}) & 0xFF == ord('${button}')`)
 
    }  
       //% block="---"
       export function noteSep3() {

    }
    //% block="关闭VideoCapture对象[VD]" blockType="command"
    //% VD.shadow="normal" VD.defl="vd"
    export function Close(parameter: any, block: any) {
        let vd=parameter.VD.code;
        Generator.addCode(`${vd}.release()`)
  
    
    } 

    //% block="销毁全部窗口" blockType="command"
    export function Destroy(parameter: any, block: any) {
        Generator.addCode(`cv2.destroyAllWindows()`)
  
    
    } 

    //% block="---"
    export function noteSep4() {

    }
 
    //% block="创建级联分类器[CC]" blockType="command"
    //% CC.shadow="normal" CC.defl="casecade"
   
    export function CascadeClassifier(parameter: any, block: any) {
        let cc=parameter.CC.code;
      
        Generator.addCode(`${cc} = cv2.CascadeClassifier()`)
 
    } 
    //% block="对象名[CC]加载[MODE]模型" blockType="command"
    //% CC.shadow="normal" CC.defl="casecade"
    //% MODE.shadow="dropdown" MODE.options="MODE"
    export function loadCascadeClassifier(parameter: any, block: any) {
        let cc=parameter.CC.code;
        let motion=parameter.MODE.code;
        if(`${motion}` === '1'){

            Generator.addCode(`${cc}.load(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")`)
        }else if(`${motion}` === '2'){

            Generator.addCode(`${cc}.load("haarcascade_car.xml")`)
        }
        
 
    }   
    //% block="对象名[CC]是否加载完成" blockType="boolean"
    //% CC.shadow="normal" CC.defl="casecade"
   
    export function load(parameter: any, block: any) {
        let cc=parameter.CC.code;
      
        Generator.addCode(`not ${cc}.empty()`)
 
    }  
    //% block="对象名[CC]输入检测图像[IMG]返回检测结果信息" blockType="reporter"
    //% CC.shadow="normal" CC.defl="casecade"
    //% IMG.shadow="normal" IMG.defl="img_src"
    export function detection(parameter: any, block: any) {
        let cc=parameter.CC.code;
        let img=parameter.IMG.code;
        Generator.addCode(`${cc}.detectMultiScale(${img}, 1.5, 3)`)
 
    }      
    //% block="在图片[IMG]上绘制一个起始点[XY]结束点[WH]颜色[COLOR]的矩形" blockType="command"
    //% IMG.shadow="normal" IMG.defl="img_src"
    //% XY.shadow="normal" XY.defl="x,y"
    //% WH.shadow="normal" WH.defl="x,y"
    //% COLOR.shadow="normal" COLOR.defl="x,y"
    //% COLOR.shadow="colorPalette" 
    export function line(parameter: any, block: any) {
        let img=parameter.IMG.code;
        let xy=parameter.XY.code;
        let wh=parameter.WH.code;
        let color=parameter.COLOR.code; //获取传进来的值

        var r = 0;
        var g = 0;
        var b = 0;
        try {
            if ( color.length == 8 ) {//分别截取RGB值然后转换为数字值
                r = parseInt(color.substring(2, 4), 16);
                g = parseInt(color.substring(4, 6), 16);
                b = parseInt(color.substring(6, 8), 16);
            }
        } catch(e) {
            return '';
        }

        Generator.addCode(`cv2.rectangle(${img}, (${xy}), (${wh}), (${b},${g},${r}), 2, cv2.FILLED)`)
 
    }      
}


